package com.example.backend.config;

import com.example.backend.domain.entity.User;
import com.example.backend.domain.enums.Role;
import com.example.backend.domain.enums.UserStatus;
import com.example.backend.repository.UserRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;

@Configuration
public class DataSeeder {
    
    @Bean
    @Transactional
    public CommandLineRunner initData(UserRepository userRepository, PasswordEncoder passwordEncoder) {
        return args -> {
            // 检查是否已有用户数据
            if (userRepository.count() > 0) {
                System.out.println("数据库已有用户数据，跳过初始化");
                return;
            }
            
            System.out.println("开始初始化用户数据...");
            
            // 创建管理员用户
            User admin = new User();
            admin.setUsername("admin");
            admin.setPassword(passwordEncoder.encode("admin123"));
            admin.setFullName("系统管理员");
            admin.setEmail("admin@example.com");
            admin.setPhone("13800138000");
            admin.setDepartment("IT部门");
            admin.setPosition("系统管理员");
            admin.setRole(Role.ADMIN);
            admin.setStatus(UserStatus.ACTIVE);
            admin.setCreatedAt(Instant.now());
            admin.setUpdatedAt(Instant.now());
            userRepository.save(admin);
            
            // 创建客服用户
            User agent1 = new User();
            agent1.setUsername("agent1");
            agent1.setPassword(passwordEncoder.encode("agent123"));
            agent1.setFullName("客服小王");
            agent1.setEmail("agent1@example.com");
            agent1.setPhone("13800138001");
            agent1.setDepartment("客服部");
            agent1.setPosition("客服专员");
            agent1.setRole(Role.AGENT);
            agent1.setStatus(UserStatus.ACTIVE);
            agent1.setCreatedAt(Instant.now());
            agent1.setUpdatedAt(Instant.now());
            userRepository.save(agent1);
            
            // 创建客户用户
            User customer1 = new User();
            customer1.setUsername("customer1");
            customer1.setPassword(passwordEncoder.encode("customer123"));
            customer1.setFullName("客户张三");
            customer1.setEmail("customer1@example.com");
            customer1.setPhone("13800138003");
            customer1.setRole(Role.CUSTOMER);
            customer1.setStatus(UserStatus.ACTIVE);
            customer1.setCreatedAt(Instant.now());
            customer1.setUpdatedAt(Instant.now());
            userRepository.save(customer1);
            
            System.out.println("用户数据初始化完成！");
            System.out.println("管理员账号: admin / admin123");
            System.out.println("客服账号: agent1 / agent123");
            System.out.println("客户账号: customer1 / customer123");
        };
    }
}


